﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.CommonFilter;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.JSON;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;

namespace WSDZ.KM.ZH.ApproveTypeChangeOfSalNewChange3
{
    [Description("销售订单新变更单审批类型切换"), HotUpdate]
    public class ApproveTypeChange : AbstractDynamicFormPlugIn
    {
        public string btype = "销售订单新变更单审批类型切换";
        public string entyKey = "FSaleOrderEntry";

        public override void DataChanged(DataChangedEventArgs e)
        {
          
            if (e.Field.Key.ToStringV1().EqualsV1("F_RLKX_splx"))
            {
                try
                {
                    string zt = Model.GetValue("FDocumentStatus").ToStringV1();
                    if (!zt.EqualsV1("C"))
                    {
                        string newValue = e.NewValue.ToStringV1();
                        ChangeStatusByApproveType(newValue);
                    }
            
                }
                catch (Exception)
                {
                    throw;
                }
            }
            base.DataChanged(e);
        }
        public override void AfterBindData(EventArgs e)
        {
            base.AfterBindData(e);
            string zt = Model.GetValue("FDocumentStatus").ToStringV1();
            if (!zt.EqualsV1("C"))
            {
                string splx = Model.GetValue("F_RLKX_splx").ToStringV1();
                ChangeStatusByApproveType(splx);
            }
        }
        public void ChangeStatusByApproveType(string splx)
        {
            //销售订单新变更单
            //根据单据头选择不同的审批类型，锁定和放开字段：
            //1、变更交期（明细出货日期字段放开，明细行其他字段锁定，其他页签全部字段锁定）
            //2、变更型号（明细行除了变更类型为新增的行，其他变更类型的行全部字段锁定）
            //3、变更数量（明细销售数量字段放开，明细行其他字段锁定，其他页签全部字段锁定）
            //4、订单关闭（除了明细行的变更类型字段外，单据体其他全部字段锁定）
            //5、变更收款计划（只有收款计划页签字段不锁定，其他单据体页签的字段全部锁定）
            //6、变更含税单价（明细含税单价字段放开，明细行其他字段锁定，其他页签全部字段锁定）
            //7、变更制造单（只有制造单页签字段不锁定，其他单据体页签的字段全部锁定）
            //8、同时变更(不锁定字段)。注意：切换审批类型的时候要同步生效控制


            Entity entryEntity = this.View.BillBusinessInfo.GetEntryEntity(entyKey);
            List<Field> fieldList = entryEntity.Fields.ToList();
            if (splx.IsNullOrEmptyOrWhiteSpace())
            {
                View.GetControl("FSaleOrderClause").Enabled = false;
                View.GetControl("FSaleOrderPlan").Enabled = false;
                View.GetControl("FSaleOrderPlanEntry").Enabled = false;
                View.GetControl("FSalOrderTrace").Enabled = false;
                View.GetControl("FSalOrderTraceDetail").Enabled = false;
                View.GetControl("F_RLKX_XSDDMXZZD").Enabled = false;
                View.GetControl("F_RLKX_scztSubEntity").Enabled = false;
                View.GetControl("FSaleOrderFinance").Enabled = false;
                View.GetControl("FSalOrderRec").Enabled = false;
                View.GetControl("FOrderEntryPlan").Enabled = false;
                View.GetControl("FRecMentEntry").Enabled = false;
                View.GetControl("FTaxDetailSubEntity").Enabled = false;
                View.GetControl(entyKey).Enabled = false;
                View.GetControl("FChangeType").Enabled = true;
            }
            //变更交期
            else if (splx.EqualsV1("A"))
            {
        
                View.GetControl("FSaleOrderClause").Enabled = false;
                View.GetControl("FSaleOrderPlan").Enabled = false;
                View.GetControl("FSaleOrderPlanEntry").Enabled = false;
                View.GetControl("FSalOrderTrace").Enabled = false;
                View.GetControl("FSalOrderTraceDetail").Enabled = false;
                View.GetControl("F_RLKX_XSDDMXZZD").Enabled = false;
                View.GetControl("F_RLKX_scztSubEntity").Enabled = false;
                View.GetControl("FSaleOrderFinance").Enabled = false;
                View.GetControl("FSalOrderRec").Enabled = false;
                View.GetControl("FOrderEntryPlan").Enabled = false;
                View.GetControl("FRecMentEntry").Enabled = false;
                View.GetControl("FTaxDetailSubEntity").Enabled = false;

                View.GetControl(entyKey).Enabled = false;
                View.GetControl("FDeliveryDate").Enabled = true;
                View.GetControl("FChangeType").Enabled = true;
            }
            //变更型号
            else if (splx.EqualsV1("B"))
            {
                View.GetControl(entyKey).Enabled = true;
                View.GetControl("FChangeType").Enabled = true;
                //View.GetControl("FSaleOrderClause").Enabled = false;
                //View.GetControl("FSaleOrderPlan").Enabled = false;
                //View.GetControl("FSaleOrderPlanEntry").Enabled = false;
                //View.GetControl("FSalOrderTrace").Enabled = false;
                //View.GetControl("FSalOrderTraceDetail").Enabled = false;
                //View.GetControl("F_RLKX_XSDDMXZZD").Enabled = false;
                //View.GetControl("F_RLKX_scztSubEntity").Enabled = false;
                //View.GetControl("FSaleOrderFinance").Enabled = false;
                //View.GetControl("FSalOrderRec").Enabled = false;
                //View.GetControl("FOrderEntryPlan").Enabled = false;
                //View.GetControl("FRecMentEntry").Enabled = false;
                //View.GetControl("FTaxDetailSubEntity").Enabled = false;
                //View.GetControl("FDeliveryDate").Enabled = false;
                //View.GetControl(entyKey).Enabled = true;

                //Entity entity = this.View.BusinessInfo.GetEntity(entyKey);
                //DynamicObjectCollection entyDatas= Model.GetEntityDataObject(entity);
                //if (entyDatas != null)
                //{
                //    for (int i = 0; i < entyDatas.Count; i++)
                //    {
                //        DynamicObject entyObj = entyDatas[i];
                //        int ser = entyObj["Seq"].ToStringV1().ToInt32V1();
                //        string changeType = entyObj["ChangeType"].ToStringV1();
                //        if (changeType.EqualsV1("A"))
                //        {
                //            LockEntityRow(ser, entyKey, true);
                //        }
                //        else
                //        {
                //            LockEntityRow(ser, entyKey, false);
                //        }

                //    }
                //}
                //View.UpdateView(entyKey);

            }
            //变更数量
            else if (splx.EqualsV1("C"))
            {
                View.GetControl("FSaleOrderClause").Enabled = false;
                View.GetControl("FSaleOrderPlan").Enabled = false;
                View.GetControl("FSaleOrderPlanEntry").Enabled = false;
                View.GetControl("FSalOrderTrace").Enabled = false;
                View.GetControl("FSalOrderTraceDetail").Enabled = false;
                View.GetControl("F_RLKX_XSDDMXZZD").Enabled = false;
                View.GetControl("F_RLKX_scztSubEntity").Enabled = false;
                View.GetControl("FSaleOrderFinance").Enabled = false;
                View.GetControl("FSalOrderRec").Enabled = false;
                View.GetControl("FOrderEntryPlan").Enabled = false;
                View.GetControl("FRecMentEntry").Enabled = false;
                View.GetControl("FTaxDetailSubEntity").Enabled = false;

                View.GetControl(entyKey).Enabled = false;
                View.GetControl("FQty").Enabled = true;
                View.GetControl("FChangeType").Enabled = true;
            }
            //订单关闭
            else if (splx.EqualsV1("D"))
            {
                View.GetControl(entyKey).Enabled = false;
                View.GetControl("FChangeType").Enabled = true;
            }
            //变更收款计划
            else if (splx.EqualsV1("E"))
            {
                View.GetControl(entyKey).Enabled = false;
                View.GetControl("FSaleOrderClause").Enabled = false;
                View.GetControl("FSalOrderTrace").Enabled = false;
                View.GetControl("FSalOrderTraceDetail").Enabled = false;
                View.GetControl("F_RLKX_XSDDMXZZD").Enabled = false;
                View.GetControl("F_RLKX_scztSubEntity").Enabled = false;
                View.GetControl("FSaleOrderFinance").Enabled = false;
                View.GetControl("FSalOrderRec").Enabled = false;
                View.GetControl("FOrderEntryPlan").Enabled = false;
                View.GetControl("FRecMentEntry").Enabled = false;
                View.GetControl("FTaxDetailSubEntity").Enabled = false;
                View.GetControl("FSaleOrderPlan").Enabled = true;
                View.GetControl("FChangeType").Enabled = true;
            }
            //变更含税单价
            else if (splx.EqualsV1("F"))
            {
                View.GetControl("FSaleOrderClause").Enabled = false;
                View.GetControl("FSaleOrderPlan").Enabled = false;
                View.GetControl("FSaleOrderPlanEntry").Enabled = false;
                View.GetControl("FSalOrderTrace").Enabled = false;
                View.GetControl("FSalOrderTraceDetail").Enabled = false;
                View.GetControl("F_RLKX_XSDDMXZZD").Enabled = false;
                View.GetControl("F_RLKX_scztSubEntity").Enabled = false;
                View.GetControl("FSaleOrderFinance").Enabled = false;
                View.GetControl("FSalOrderRec").Enabled = false;
                View.GetControl("FOrderEntryPlan").Enabled = false;
                View.GetControl("FRecMentEntry").Enabled = false;
                View.GetControl("FTaxDetailSubEntity").Enabled = false;

                View.GetControl(entyKey).Enabled = false;
                View.GetControl("FTaxPrice").Enabled = true;
                View.GetControl("FChangeType").Enabled = true;
            }
            //变更制造单
            else if (splx.EqualsV1("G"))
            {
                View.GetControl("FSaleOrderClause").Enabled = false;
                View.GetControl("FSaleOrderPlan").Enabled = false;
                View.GetControl("FSaleOrderPlanEntry").Enabled = false;
                View.GetControl("FSalOrderTrace").Enabled = false;
                View.GetControl("FSalOrderTraceDetail").Enabled = false;
                View.GetControl("F_RLKX_scztSubEntity").Enabled = false;
                View.GetControl("FSaleOrderFinance").Enabled = false;
                View.GetControl("FSalOrderRec").Enabled = false;
                View.GetControl("FOrderEntryPlan").Enabled = false;
                View.GetControl("FRecMentEntry").Enabled = false;
                View.GetControl("FTaxDetailSubEntity").Enabled = false;
                View.GetControl(entyKey).Enabled = false;
                View.GetControl("F_RLKX_XSDDMXZZD").Enabled = true;
                View.GetControl("FChangeType").Enabled = true;
            }
            //同时变更
            else if (splx.EqualsV1("H"))
            {
                View.GetControl("FSaleOrderClause").Enabled = true;
                View.GetControl("FSaleOrderPlan").Enabled = true;
                View.GetControl("FSaleOrderPlanEntry").Enabled = true;
                View.GetControl("FSalOrderTrace").Enabled = true;
                View.GetControl("FSalOrderTraceDetail").Enabled = true;
                View.GetControl("F_RLKX_scztSubEntity").Enabled = true;
                View.GetControl("FSaleOrderFinance").Enabled = true;
                View.GetControl("FSalOrderRec").Enabled = true;
                View.GetControl("FOrderEntryPlan").Enabled = true;
                View.GetControl("FRecMentEntry").Enabled = true;
                View.GetControl("FTaxDetailSubEntity").Enabled = true;
                View.GetControl(entyKey).Enabled = true;
                View.GetControl("F_RLKX_XSDDMXZZD").Enabled = true;
                View.GetControl("FChangeType").Enabled = true;
            }
        }

        public void LockEntityRow(int rowIndex, string entityKey, bool isEnabled)
        {
            Entity entryEntity = this.View.BillBusinessInfo.GetEntryEntity(entityKey);
            List<Field> lockFields = entryEntity.Fields.ToList();
            List<string> keys = lockFields.Select(x => x.Key).Distinct().ToList();
            //string str = string.Join(";", keys);
            foreach (var field in lockFields)
            {
                if (rowIndex >= 0)
                {
                    View.GetFieldEditor(field.Key, rowIndex).Enabled = isEnabled;
                    //if (isEnabled)
                    //{
                    //    View.UpdateView(entyKey,rowIndex);
                    //}

                }
                //else
                //    View.GetControl<FieldEditor>(field.Key).Enabled = isEnabled;


                //if (rowIndex >= 0)
                //    View.GetFieldEditor(field.Key, rowIndex).SetEnabled("",isEnabled);

            }
            //rowIndex = rowIndex - 1;
            //var entity = this.View.BusinessInfo.GetEntryEntity(entityKey);
            //List<Field> lockFields = entity.Fields.ToList();
            //var rowData = this.Model.GetEntityDataObject(entity, rowIndex);
            //foreach (var field in lockFields)
            //{
            //     View.StyleManager.SetEnabled(field.Key, rowData, null, isEnabled);
            //}
            //View.UpdateView(entyKey);




        }


    }

}
